/**
 * 导出接口
 * create by 张康 on 2021-03-16 15:02:31
 */
import request from "@/utils/request";
import { Loading } from 'element-ui';
let exportApi = {
  /**
   * 下载附件
   * @param url
   * @param name
   */
  downloadFile({ url, name }) {
    console.log(url,);
    if (url) {
      request.get(`${url}`, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
          'appVersion': '1.0.0',
          'framework': 'qb'
        },
        responseType: "blob",
      }).then(res => {
        let loading = Loading.service({
          lock: true,
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.8)",
          text: "下载中，请稍后..."
        });
        this.download(res.data, name, loading);
      })
    }
  },
  /**
   * 可以作为公共模块提供，对外访问
   * @param data
   * @param filename
   */
  download: function (data, filename, loading) {
  
    let blob = new Blob([data], {
      type: "application/octet-stream"
    });
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(blob, filename);
    } else {
      var blobURL = window.URL.createObjectURL(blob);
      var tempLink = document.createElement("a");
      tempLink.style.display = "none";
      tempLink.href = blobURL;
      tempLink.setAttribute("download", filename);
      if (typeof tempLink.download === "undefined") {
        tempLink.setAttribute("target", "_blank");
      }
      document.body.appendChild(tempLink);
      tempLink.click();
      document.body.removeChild(tempLink);
      window.URL.revokeObjectURL(blobURL);
    }
    loading && loading.close();
  },
};
export default exportApi;
